/* hal_gpio.c */
#include "hal_gpio.h"

void GPIO_WritePortOutputLevel(GPIO_Type * base, uint32_t val)
{
    base->PDOR = val;
}

uint32_t GPIO_ReadPortInputLevel(GPIO_Type * base)
{
    return base->PDIR;
}

void GPIO_SetPinsOutputLevel(GPIO_Type * base, uint32_t pins, bool high)
{
    if (high)
    {
        base->PSOR = pins;
    }
    else
    {
        base->PCOR = pins;
    }
}

void GPIO_TogglePinsOutputLevel(GPIO_Type * base, uint32_t pins)
{
    base->PTOR = pins;
}

void GPIO_EnablePinsOutputDirection(GPIO_Type * base, uint32_t pins, bool enable)
{
    if (enable)
    {
        base->POER |= pins;
    }
    else
    {
        base->POER &= ~pins;
    }
}

void GPIO_EnablePinsInputDirection(GPIO_Type * base, uint32_t pins, bool enable)
{
    if (enable)
    {
        base->PIER |= pins;
    }
    else
    {
        base->PIER &= ~pins;
    }
}

uint32_t GPIO_GetPinEventStatusFlags(GPIO_Type * base)
{
    return base->PIFR;
}

void GPIO_ClearPinEventStatusFlags(GPIO_Type * base, uint32_t flags)
{
    base->PIFR = flags;
}

void GPIO_SetPinDigitalInputConf(GPIO_Type * base, uint32_t pin, GPIO_PinDigitalInputEventConf_Type * conf)
{
    base->PCR[pin] = 0u;
    
    if (conf)
    {
        uint32_t tmp32 = GPIO_PCR_IRQC(conf->InputEventReq);
        if (conf->DigitalFilterWidth > 0u)
        {
            tmp32 |= (GPIO_PCR_DFE_MASK | GPIO_PCR_DFW(conf->DigitalFilterWidth));
        }
        
        if ( conf->EnableInvertInputLogic)
        {
            tmp32 |= GPIO_PCR_INVE_MASK;
        }
        
        base->PCR[pin] = tmp32;
    }
}

/* EOF. */

